package com.open.system.service.impl;

import com.open.system.mapper.UserRoleMapper;
import com.open.common.model.Page;
import com.open.common.model.Sort;
import com.open.system.model.UserRole;
import com.open.system.service.IUserRoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 菜单信息
 *
 * @author 大树03
 * @date 2025-02-16 21:27
 */
@Service(value = "userRoleService")
@Slf4j
public class UserRoleServiceImpl implements IUserRoleService {

	private UserRoleMapper userRoleMapper;

    @Autowired
    public void setUserRoleMapper(UserRoleMapper userRoleMapper) {
        this.userRoleMapper = userRoleMapper;
    }

    @Override
    @Transactional()
    public Boolean save(UserRole model)  {
        return userRoleMapper.save(model);
    }

    @Override
    @Transactional()
    public Boolean update(UserRole model)  {
        return userRoleMapper.update(model);
    }

	@Override
	public Page<UserRole> findPage(Page<UserRole> page,UserRole item)  {
        Map<String,Object> map = new HashMap<String, Object>(4);
        int startPoint = (page.getIndex()-1)*page.getSize();
        map.put("orderBy",new Sort());
        map.put("item",item);
        map.put("startPoint",startPoint);
        map.put("endPoint",page.getSize());
        Integer count = userRoleMapper.findPageCount(map);
        if (count > startPoint) {
            page.setItems(userRoleMapper.findPage(map));
        }else {
            page.setItems(new ArrayList<UserRole>());
        }
        page.setTotal(count);
        return page;
    }

	@Override
    public List<UserRole> findByModel(UserRole item)  {
        Map<String,Object> map=new HashMap<String, Object>(1);
        map.put("item",item);
        return userRoleMapper.findByModel(map);
    }

	@Override
	public List<UserRole> findByUserId(Long pk)  {
        return userRoleMapper.findByUserId(pk);
    }

    @Override
    public Integer countByModel(UserRole item){
        Map<String,Object> map=new HashMap<String, Object>(1);
        map.put("item",item);
        return userRoleMapper.findPageCount(map);
    }

    @Override
    @Transactional()
    public Boolean removeByUserId(List<Long> pks) {
        for (Long pk : pks) {
            userRoleMapper.removeByUserId(pk);
        }
        return true;
    }
}